import qs from 'qs'
import Cookies from 'js-cookie'
import request from '@/utils/request'
import { TokenType } from '@/utils/enum'

const url = '/oauth/token'
class User {
  static login(data) {
    return request({
      url,
      method: 'post',
      headers: { 'content-type': 'application/x-www-form-urlencoded' },
      params: {
        grant_type: 'password',
        client_id: 'kancha_admin',
        client_secret: '8973b3700bc249e885b963f1b74721d5'
      },
      data: qs.stringify(data)
    })
  }
  static vworkLogin(code) {
    return request({
      url,
      method: 'post',
      headers: { 'content-type': 'application/x-www-form-urlencoded' },
      params: {
        grant_type: 'vwork',
        client_id: 'ww02d18805a51bd9bb:1000050',
        client_secret: '02985881abc344ca96a2f4e3bb107788',
        code: code
      }
    })
  }
  static refresh() {
    return request({
      url,
      method: 'post',
      params: {
        grant_type: 'refresh_token',
        client_id: 'adminClient',
        client_secret: 'b5402e2a28134f1e85419d3b24fbc3fc',
        refresh_token: Cookies.get(TokenType.USER_REFRESHTOKEN)
      }
    })
  }
  static getMenus(userId) {
    return request({
      url: '/rbac/codes/privilege/'+userId,
      method: 'get'
    })
  }
  static changePassword(data) {
    return request({
      url: '/users/password',
      method: 'put',
      data
    })
  }
  static getUserInfo(id) {
    return request({
      url: '/users/' + id,
      method: 'get'
    })
  }
}

export default User
